package Problem;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Earth extends JLabel implements ActionListener{
    JLabel moon;
    Timer timer;
    double pointx[]=new double[360],pointy[]=new double[360];
    int w = 200,h=200,i=0;
    Earth(){
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(w, h));
        timer = new Timer(20, this);
        setIcon(new ImageIcon("src/chapter12/earth.jpg"));
        setHorizontalAlignment(SwingConstants.CENTER);
        moon = new JLabel(new ImageIcon("src/chapter12/moon.jpg"),SwingConstants.CENTER);
        add(moon);
        moon.setPreferredSize(new Dimension(60, 60));
        pointx[0]=0;
        pointy[0]=h/2;
        double angle = 1*Math.PI/180;
        for (int i = 0; i < 359; i++) {
            pointx[i+1] =pointx[i]*Math.cos(angle) - Math.sin(angle)*pointy[i];
            pointy[i+1]=pointy[i]*Math.cos(angle)+pointx[i]*Math.sin(angle);
        }
        for (int i = 0; i < 360; i++) {
            pointx[i] =0.8*pointx[i]+w/2;
            pointy[i]=0.8*pointy[i]+h/2;

        }
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        i =(i+1)%360;
        moon.setLocation((int)pointx[i]-30, (int)pointy[i]-30);

    }


}

